<?php
    class Product extends MY_Controller {

        public function index()
        {
            //获取分类列表
            $this->load->model('admin/Sort_model');
            $treelist=$this->Sort_model->get_sort_all();
            $data['treelist']=$treelist;


            $dopost = $this->input->get('dopost',true); 
            // 分类ID
            $typeid = $this->input->get('typeid',true); 
            //关键字
            $keyword = $this->input->get('keyword',true); 
            //属性
            $flag = $this->input->get('flag',true); 
            //是否取分类及子分类(0是1否)
            $typeflag = $this->input->get('typeflag',true); 


            $whereSql =" WHERE arc.typeid=sot.id ";
            if($keyword != '')
            {
                $whereSql .= " AND ( CONCAT(arc.title,arc.writer) LIKE '%$keyword%') ";
            }
            if($flag != '')
            {
                $whereSql .= " AND FIND_IN_SET('$flag', arc.flag) ";
            }
            if($typeid != 0&&$typeid !='')
            {
                if($typeflag==0)
                {
                    $typeid=$typeid.','.$this->_sort_id_child_list($typeid);
                    $typeid=rtrim($typeid,',');
                    $whereSql .= ' AND arc.typeid IN ('.$typeid.')';
                }
                else
                {
                    $whereSql .= ' AND arc.typeid = ('.$typeid.')';
                }
            }


            //查询分页数据sql
            $query = "SELECT sot.name as cname,arc.* FROM `crazy_product` arc,`crazy_sort` sot $whereSql ORDER BY arc.uptime DESC";
            //获取总数sql语句
            $query_count = "SELECT count(*) as total FROM `crazy_product` arc,`crazy_sort` sot $whereSql ORDER BY arc.uptime DESC";
            $list_count=$this->db->query($query_count)->row();    
            //从数据库中获取列表总数
            $num=$list_count->total;  
            $search_str="dopost=$dopost&typeid=$typeid&keyword=$keyword&flag=$flag";
            //初始化分页类数据
            $params = array('total' => $num, 'listRows' => '15','pa'=>$search_str);
            $this->load->library('Page',$params);
            $query= $query."  limit {$this->page->limit} ";
            $reslut=$this->db->query($query);
            $data['list']=$reslut;
            $data['page']= $this->page->fpage();

            //获取自定义属性列表   
            $this->load->model('admin/Archives_model');
            $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();

            $this->load->view('admin/product_index',$data);
        }

        //新增产品
        public function add()
        {
            //获取分类          
            $this->load->model('admin/Sort_model');
            $treelist=$this->Sort_model->get_sort_all();
            $data['treelist']=$treelist;

            //获取自定义属性列表   
            $this->load->model('admin/Archives_model');
            $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();



            $this->load->view('admin/product_add',$data);
        }

        public function save()
        {
            $this->load->helper('common_function');
            $dopost = $this->input->post('dopost',true); 
            $this->load->model('admin/Product_model');
            //加载CI日期辅助函数
            $this->load->helper('date');
            if($dopost=='add')
            {  
                //添加数据


                //分类ID
                $typeid = $this->input->post('typeid',true); 
                //自定义属性
                $flags =$this->input->post('flags',true);
                $flag = !empty($flags) ? join(',',$flags) : '';

                //标题
                $title = $this->input->post('title',true); 
                //作者
                $writer = $this->input->post('writer',true); 
                //来源
                $source = $this->input->post('source',true); 
                //内容摘要
                $description = $this->input->post('description',true); 
                //内容
                $arcids = $this->input->post('content',true); 
                //图片地址
                $litpic = $this->input->post('url1',true);
                //发布时间
                $sendtime = human_to_unix($this->input->post('sendtime',true));
                //更新时间
                $uptime = now();
                //创建时间
                $createtime = now(); 
                //产品价格
                $price=  $this->input->post('price',true); 
                //产品规格
                $norm=  $this->input->post('norm',true); 
                //产品品牌
                $brand=  $this->input->post('brand',true); 
                //货币单位
                $crunit=  $this->input->post('crunit',true); 
                //产品单位
                $prunit=  $this->input->post('prunit',true); 
                //产品图片地址

                $bigpic="";
                $file_info=$this->session->userdata('file_info'); 
                if($file_info)
                {
                    foreach($file_info as $k=>$v)
                    {
                        $bigpic=$bigpic."$v|";
                    } 
                }
                $bigpic=  rtrim($bigpic,'|'); 

                $data = array(
                    'typeid' => $typeid ,
                    'flag' => $flag,
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'description' => $description,
                    'arcids' => $arcids,
                    'litpic' => $litpic,
                    'sendtime' => $sendtime,
                    'uptime' => $uptime,
                    'createtime' => $createtime,
                    'price'=>$price,
                    'norm'=>$norm,
                    'brand'=>$brand,
                    'crunit'=>$crunit,
                    'prunit'=>$prunit,
                    'bigpic'=>$bigpic

                );

                $this->Product_model->product_save_add($data);
                //清除图片session
                $this->_img_session();
                ShowMsg('添加成功!', site_url("admin/product"),0,1000);
                exit; 
            }
            if($dopost=='modify')
            {

                //编辑数据
                $id= $this->input->post('id',true);
                //分类ID
                $typeid = $this->input->post('typeid',true); 
                //自定义属性
                $flags =$this->input->post('flags',true);
                $flag = !empty($flags) ? join(',',$flags) : '';

                //标题
                $title = $this->input->post('title',true); 
                //作者
                $writer = $this->input->post('writer',true); 
                //来源
                $source = $this->input->post('source',true); 
                //内容摘要
                $description = $this->input->post('description',true); 
                //内容
                $arcids = $this->input->post('content',true); 
                //图片地址
                $litpic = $this->input->post('url1',true);
                //发布时间
                $sendtime = human_to_unix($this->input->post('sendtime',true));
                //更新时间
                $uptime = now();
                //产品价格
                $price=  $this->input->post('price',true); 
                //产品规格
                $norm=  $this->input->post('norm',true); 
                //产品品牌
                $brand=  $this->input->post('brand',true); 
                //货币单位
                $crunit=  $this->input->post('crunit',true); 
                //产品单位
                $prunit=  $this->input->post('prunit',true); 
                //产品图片地址
                $bigpic="";
                $file_info=$this->session->userdata('file_info'); 
                if($file_info)
                {
                    foreach($file_info as $k=>$v)
                    {
                        $bigpic=$bigpic."$v|";
                    } 
                }
                $bigpic=  rtrim($bigpic,'|');  

                $data = array(
                    'typeid' => $typeid ,
                    'flag' => $flag,
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'description' => $description,
                    'arcids' => $arcids,
                    'litpic' => $litpic,
                    'sendtime' => $sendtime,
                    'uptime' => $uptime,
                    'price'=>$price,
                    'norm'=>$norm,
                    'brand'=>$brand,
                    'crunit'=>$crunit,
                    'prunit'=>$prunit,
                    'bigpic'=>$bigpic

                );

                $this->Product_model->get_product_modify($id,$data);
                //清除图片session
                $this->_img_session();
                ShowMsg('编辑成功!', site_url("admin/product"),0,1000);
                exit; 
            }

        }


        public function modify()
        {

            $this->load->helper('common_function');
            $id = $this->input->get('id',true);  
            $this->load->model('admin/Product_model');
            $model=$this->Product_model->get_product_by_id_one($id);
            if($model)
            {
                //获取分类
                $this->load->model('admin/Sort_model');
                $treelist=$this->Sort_model->get_sort_all();
                $data['treelist']=$treelist;

                //获取自定义属性列表 
                $this->load->model('admin/Archives_model');  
                $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();

                $data['model']=$model;
                $this->load->view('admin/product_modify',$data);

            }
            else
            {
                ShowMsg('数据不存在!',-1,0,1000);
                exit; 
            } 
        } 


        public function del()
        {
            $this->load->helper('common_function');
            $qstr = $this->input->get('qstr',true);
            $aid = $this->input->get('aid',true);    
            $dopost = $this->input->get('dopost',true);    
            $arcids = preg_replace("#[^0-9,]#", '', preg_replace("#`#", ',', $qstr));
            $arr_arcids=explode(',',$arcids);
            $this->load->model('admin/Product_model');

            foreach($arr_arcids as $var)
            {

                $this->Product_model->get_product_del($var);
            } 
            ShowMsg('删除成功!', site_url("admin/product"),0,1000);
            exit; 
        }



        public function img_add()
        {
            $this->load->library('Fileupload');
            $msg='';

            if($this->fileupload->upload("Filedata"))
            {
                //获取上传返回的文件名
                $msg=$this->fileupload->getFileName();

                //保存信息到 session
                $file_info=array();
                $file_info=$this->session->userdata('file_info');
                $fileid=$this->session->userdata('fileid');

                if(!$fileid)
                {
                    $fileid=1;
                }
                else
                {
                    $fileid=$fileid+1;
                }
                $file_info[$fileid]=$msg;
                $this->session->set_userdata('fileid',$fileid);
                $this->session->set_userdata('file_info',$file_info);
                echo "FILEID:".$fileid;
                exit(0);

            }
            else
            {
                $msg=$this->fileupload->getErrorMsg();
                exit ('FILEID:'.$msg);
            }




        }

        public function img_session()
        {
            $this->session->unset_userdata('file_info');
            $this->session->unset_userdata('fileid');
        }
        public function _img_session()
        {
            $this->session->unset_userdata('file_info');
            $this->session->unset_userdata('fileid');
        }

        public function img_all()
        {   
            $file_info=$this->session->userdata('file_info'); 
            foreach($file_info as $k=>$v)
            {
                echo $k.'=>'.$v.'<br/>';
            } 
        }

        public function img_handle()
        {
            $dopost=$_GET['dopost'];
            if($dopost=='thumbnail')
            {
                $id=$_GET['id'];
                $file_info=$this->session->userdata('file_info');  
                if( empty($id) )
                {
                    header('HTTP/1.1 500 Internal Server Error');
                    echo 'No ID';
                    exit(0);
                }
                if (!$file_info)
                {
                    header('HTTP/1.1 404 Not found');
                    exit(0);
                }
                header('Content-type: image/jpg');
                echo file_get_contents('./public/uploads/'.$file_info[$id]);
                exit(0);
            }
            else if($dopost=='del')
            {

                //删除新增图片

                $id=$_GET['id'];
                $file_info=$this->session->userdata('file_info');
                if(!isset($file_info[$id]))
                {
                    echo '';
                    exit();
                }

                $imgfile='./public/uploads/'.$file_info[$id];
                if(file_exists($imgfile))
                {
                    @unlink($imgfile);
                    unset($file_info[$id]);
                    $this->session->set_userdata('file_info',$file_info);
                }




                exit;
            }
            else if($dopost=='delold')
            {
                //删除原有图片
                $id=$_GET['id'];
                $picfile=$_GET['picfile'];
                $file_info=$this->session->userdata('file_info');
                if(file_exists($picfile))
                {
                    unlink($picfile);
                    unset($file_info[$id]);
                    $this->session->set_userdata('file_info',$file_info);
                }

                exit;
            }
        }

        //返回与某个目相关的下级目录的类目ID列表
        public function _sort_id_child_list($id)
        {
            $total_id='';
            $this->load->model('admin/Sort_model');
            $model=$this->Sort_model->get_sort_by_pid($id);
            if ($model->num_rows() > 0)
            {
                foreach($model->result() as $row)
                {
                    $total_id=$total_id.$row->id.','; 
                    $total_id=$total_id.$this->_sort_id_child_list($row->id);
                }
            }

            return $total_id;
        }
    }
?>